<template>
  <div class="app-container">
      <div class="filter-container">
        <el-input clearable v-model="search.nickName" placeholder="用户名称" style="width: 170px;"></el-input>
        <el-input clearable v-model="search.phone" placeholder="手机号" style="width: 170px;margin-left:5px;"></el-input>
        
        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe 
      style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="10%" align="center" label="头像">
        <template slot-scope="scope">
          <span>
            <el-image style="width: 50px; height: 50px" :src="scope.row.headPortrait" :preview-src-list="[scope.row.headPortrait]"></el-image>
          </span>
        </template>
      </el-table-column>

      <el-table-column  min-width="10%" align="center" label="昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="手机">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="角色">
        <template slot-scope="scope">
            <span>{{scope.row.userRole | parseUserRole}}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="性别">
        <template slot-scope="scope">
            <span>{{scope.row.gender | parseUserGender}}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="注册IP">
        <template slot-scope="scope">
            <span>{{scope.row.registerIp}}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="注册时间">
        <template slot-scope="scope">
            <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="最后登录">
        <template slot-scope="scope">
            <span>{{ scope.row.lastLoginTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="签名">
        <template slot-scope="scope">
            <span>{{scope.row.describe}}</span>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="操作"  min-width="20%">
        <template slot-scope="scope">
          <router-link :to="'/usermanage/userdetails/'+scope.row.id">
            <el-button type="primary" size="small">
              查看
            </el-button>
          </router-link>
         <el-popconfirm :title="'确定要重置账号密码吗？'" icon="el-icon-info"  icon-color="red"  @onConfirm="resetAccountPassword(scope.row.id)" >
              <el-button slot="reference" type="warning"  size="small" style="margin-left:5px;">重置密码</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="getList" />
  </div>
</template>

<script>

import { deepClone,parseTime } from '@/utils'
import * as auth from '@/utils/auth'
import * as userApi from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import BxImage from '@/components/BxImage'
import guid from '@/utils/guid'
import * as imageHelper from '@/utils/image'
import * as enumHelper from '@/utils/enum'

export default {
  name: 'UserList',
  components: { Pagination,BxImage },
  data(){
    return{
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        rows: 20,
        nickName:'',
        phone:''
      },
      search:{
        nickName:'',
        phone:''
      },
      
    }
  },
  created(){
      var that=this;
      that.getList();
  },
  mounted(){

  },
  filters:{
    parseUserRole(val){
      return enumHelper.user.userRole.getName(val);
    },
    parseUserGender(val){
      return ['-','男','女'][val];
    }
  },
  methods: {
    getList(){
      var that=this;
      that.listLoading = true;
      userApi.getUserList(that.listQuery).then(res=>{
         if(res.data){
            that.list=[...res.data.items.map((item,index)=>{
              item._id=index+1;
              return item;
            })];
            that.listLoading = false;
            that.total=res.data.total|| 0;
         }
      })
    },
    searchClick(){
      var that=this;
      that.listQuery.page=1;
      that.listQuery.nickName=that.search.nickName.trim();
      that.listQuery.phone=that.search.phone.trim();
      that.getList();
    },
    resetAccountPassword(id){
       var that=this;
        userApi.resetAccountPassword({id:id}).then(res=>{
            if(res.data){
              that.$message({
                message:'重置密码成功！',
                type:'success'
              })
            }
        })
    }
  }
}
</script>
